package cn.e4j.modular.crm.controller;

import cn.e4j.core.base.controller.BaseController;
import cn.e4j.core.dto.impl.Dto;
import cn.e4j.core.dto.requestDtoResolver.DtoParam;
import cn.e4j.core.dto.requestDtoResolver.DtoWapper;
import cn.e4j.core.reqres.response.ErrorResponseData;
import cn.e4j.core.reqres.response.PageReData;
import cn.e4j.core.reqres.response.ResponseData;
import cn.e4j.core.shiro.ShiroKit;
import cn.e4j.core.shiro.ShiroUser;
import cn.e4j.core.util.DateUtils;
import cn.e4j.core.util.LangUtils;
import cn.e4j.modular.crm.entity.GwaoT;
import cn.e4j.modular.crm.entity.GwaolT;
import cn.e4j.modular.crm.service.GwaoTService;
import cn.e4j.modular.utils.CrmExceptionEnum;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.time.LocalDate;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 产品系列维护管理
 *
 * @author shixc
 * @Date 2018/01/10
 */
@Controller
@RequestMapping("/crm/gwai251")
public class Gwai251Controller extends BaseController {

    @Autowired
    private GwaoTService gwaoTService;

    /**
     * 跳转产品系列维护页面
     */
    @RequiresPermissions("crm:gwai251:init")
    @RequestMapping(value = {"", "init"})
    public String index() {
        return "/pc/crm/gwai251.html";
    }


    /**
     * 查询产品分类信息列表
     */
    @RequestMapping("/getList")
    @ResponseBody
    public ResponseData getList(@DtoParam DtoWapper dtoWapper) {
        ShiroUser user = ShiroKit.getUser();
        Page<Object> page = dtoWapper.getPage();
        Dto inDto = dtoWapper.getDto();
        inDto.put("GWAOENT", user.getDBC());
        List<Map<String, Object>> result = gwaoTService.getList(page, inDto);
        return new PageReData(page, result);
    }

    /**
     * 添加
     */
    @RequestMapping("/add")
    @RequiresPermissions("crm:gwai251:init")
    @ResponseBody
    public ResponseData add(@DtoParam DtoWapper dtoWapper) {
        ShiroUser user = ShiroKit.getUser();
        Dto inDto = dtoWapper.getDto();

        //判断产品系列编号是否重复
        GwaoT gwaot = this.gwaoTService.getByKey(inDto.getString("GWAO001"));
        if (gwaot != null) {
            return new ErrorResponseData(CrmExceptionEnum.EXISTED_THE_GWAO.getCode(),
                                            CrmExceptionEnum.EXISTED_THE_GWAO.getMessage());
        }
        GwaoT gwao = new GwaoT();
        gwao.setGWAO001(inDto.getString("GWAO001"));
        gwao.setGWAO002(inDto.getString("GWAO002"));
        gwao.setGWAO003(" ");
        gwao.setGWAOACTI(inDto.getString("GWAOACTI"));
        gwao.setGWAOENT(user.getDBC());
        gwao.setGWAOUSER(user.getACCOUNT());
        gwao.setGWAOGRUP(user.getORG_ID());
        Date date = new Date();
        gwao.setGWAOTIME(DateUtils.formatDate(date, "HH:mm:ss"));
        LocalDate today = LocalDate.now();
        gwao.setGWAODATE(today);
        gwao.setGWAOORIG(user.getORG_ID());
        gwao.setGWAOORIU(user.getACCOUNT());
        GwaolT gwaol = new GwaolT();
        gwaol.setGWAOLENT(user.getDBC());
        gwaol.setGWAOL001(gwao.getGWAO001());
        gwaol.setGWAOL002(LangUtils.getLang(user.getLANG()));
        gwaol.setGWAOL003(inDto.getString("GWAOL003"));
        gwaol.setGWAOL004(inDto.getString("GWAOL004"));
        gwaoTService.add(gwao, gwaol);
        return SUCCESS_TIP;
    }

    /**
     * 修改
     */
    @RequestMapping("/edit")
    @RequiresPermissions("crm:gwai251:init")
    @ResponseBody
    public ResponseData edit(GwaoT gwaoT, GwaolT gwaolT) {
        ShiroUser user = ShiroKit.getUser();
        //判断产品分类编号是否存在
        GwaoT gwao = this.gwaoTService.getByKey(gwaoT.getGWAO001());
        if (gwao == null) {
            return new ErrorResponseData(CrmExceptionEnum.EMPTY_THE_GWAO.getCode(),
                                            CrmExceptionEnum.EMPTY_THE_GWAO.getMessage());
        }
        gwaoT.setGWAOENT(user.getDBC());
        gwaoT.setGWAOMODU(user.getID());
        gwaoTService.edit(gwaoT,gwaolT);
        return SUCCESS_TIP;
    }

    /**
     * 删除
     */
    @RequestMapping("/del")
    @RequiresPermissions("crm:gwai251:init")
    @ResponseBody
    public ResponseData del(@DtoParam DtoWapper dtoWapper) {
        ShiroUser user = ShiroKit.getUser();
        Dto inDto = dtoWapper.getDto();
        inDto.put("GWAOENT", user.getDBC());
        gwaoTService.del(inDto);
        return SUCCESS_TIP;
    }

    /**
     * 批量删除数据
     */
    @RequestMapping("/deleteDataBatch")
    @RequiresPermissions("crm:gwai251:init")
    @ResponseBody
    public ResponseData deleteDataBatch(@DtoParam DtoWapper dtoWapper) {
        Dto inDto = dtoWapper.getDto();
        List<GwaoT> gwaoTList = inDto.getReqList(GwaoT.class);
        gwaoTService.deleteDataBatch(gwaoTList);
        return SUCCESS_TIP;
    }


}
